package com.frinder.seata.data.base;

import lombok.*;

import java.util.Objects;

/**
 * @ClassName Result
 * @Author frinder
 * @Description 统一API响应结果封装
 * @Date 2019/1/19 16:12
 * @Version 1.0
 */
@ToString
@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Result<T> {
    private int code;
    private String message;
    private T data;

    public static Result defSuccess() {
        return result(CODE.SUCCESS, null, null);
    }

    public static Result defFail() {
        return result(CODE.FAIL, null, null);
    }

    public static Result selfSuccess(String message) {
        return result(CODE.SUCCESS, message, null);
    }

    public static Result selfFail(String message) {
        return result(CODE.FAIL, message, null);
    }

    public static <T> Result dataSuccess(T data) {
        return result(CODE.SUCCESS, null, data);
    }

    public static <T> Result dataFail(T data) {
        return result(CODE.FAIL, null, data);
    }

    public static <T> Result result(CODE code, String message, T data) {
        Result<Object> result = Result.builder().code(code.getCode()).message(code.getMessage()).data(data).build();
        if (Objects.nonNull(message)) {
            result.message = message;
        }
        return result;
    }
}
